const crypt = require('crypto');

/**
 * 生成随机字符串
 * @returns {string}
 */
function getRandomStr() {
  return Math.random().toString().slice(-10);
}

/**
 * 加密信息
 * @param {string} message
 * @param {string|number} salt salt
 * @returns {string} hash hash
 * @returns {string} salt salt
 */
function cryptMsg(message = '', salt = getRandomStr()) {
  const saltMsg = `${message}:${salt}`;
  const md5 = crypt.createHash('md5');
  const hash = md5.update(saltMsg).digest('hex');

  return {
    salt,
    hash,
  };
}

module.exports = {
  cryptMsg,
};
